package com.syezon.wifikey.speed;

import java.text.DecimalFormat;

public class SizeUtils {

    public static String formartMbSize(long bytes) {
        DecimalFormat df = new DecimalFormat("##.##");
        double mb = bytes * 1.0 / 1024 / 1024;
        return df.format(mb) + "MB";
    }

    /**
     * 转换文件大小
     *
     * @return
     */
    public static SizeEntry formartkbSize(long kbSize) {
        DecimalFormat df = new DecimalFormat("####");
        if (kbSize == 0) {
            return new SizeEntry(0, "Kb/s");
        }
        if (kbSize < 1024) {
            return new SizeEntry((double) kbSize, "Kb/s");
        } else if (kbSize < 1048576) {
            return new SizeEntry((double) kbSize / 1024, "Mb/s");
        } else {
            return new SizeEntry((double) kbSize / 1048576, "Gb/s");
        }
    }

    public static String formatDouble(double value) {
        DecimalFormat decimalFormat = new DecimalFormat("##.##");
        return decimalFormat.format(value);
    }


    /**
     * 转换文件大小
     *
     * @param size
     * @return
     */
    public static SizeEntry formartSize(long size) {
        DecimalFormat df = new DecimalFormat("####");
        String wrongSize = "0B";
        if (size == 0) {
            return new SizeEntry(0, "B");
        }
        if (size < 1024) {
            return new SizeEntry((double) size, "B");
        } else if (size < 1048576) {
            return new SizeEntry((double) size / 1024, "Kb");
        } else if (size < 1073741824) {
            return new SizeEntry((double) size / 1048576, "Mb");
        } else {
            return new SizeEntry((double) size / 1073741824, "Gb");
        }
    }

    public static class SizeEntry {
        public final double value;
        public final String unit;

        public SizeEntry(double value, String unit) {
            this.value = value;
            this.unit = unit;
        }

        @Override
        public String toString() {
            return formatDouble(value) + unit;
        }
    }
}
